﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;


namespace ThorMVR.ClassModels
{
    public sealed class NHibernateHelper
    {
         private static ISessionFactory SessionFactory;


        private static void OpenSession()
        {
            Configuration configuration = new Configuration();
            configuration.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2008Dialect");
            configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
            configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, "NHibernate.Driver.SqlClientDriver");
            configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionString, "Data Source=ALTERDATA;Database=THOR00;Integrated Security=False; User ID=sa; Password=abc123");
            configuration.SetProperty(NHibernate.Cfg.Environment.ShowSql, "TRUE");
            

            configuration.AddAssembly("ThorMVR");

            SessionFactory = configuration.BuildSessionFactory();

        }

        public static ISession GetCurrentSession()
        {

            if (SessionFactory == null)

                NHibernateHelper.OpenSession();

 

            return SessionFactory.OpenSession();

        }


        public static void CloseSessionFactory()
        {

            if (SessionFactory != null)

                SessionFactory.Close();
         
        }

    
    }
}
